
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import java.io.*;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import javax.swing.ImageIcon;
import sun.audio.AudioData;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Panel.java
 *
 * Created on 4.10.2011, 22:57:58
 */
/**
 *
 * @author Riso
 */
public class Panel extends javax.swing.JPanel implements ActionListener {

    static ArrayList ponorkypole = new ArrayList();
    pozadie pozadie = new pozadie();
    Strela strela = new Strela();
    Slnko slnko = new Slnko();
    boolean pauza = false;
    int skore = 2400;
    int cas = 2400;
    Lodka l = new Lodka(this);
    Ponorka ponorka;
    Timer t = new Timer(50, this);
    ImageIcon strelalabel = new javax.swing.ImageIcon(getClass().getResource("/img/strela.png"));
    ImageIcon zivotlabel = new javax.swing.ImageIcon(getClass().getResource("/img/zivot.png"));
    JLabel ukazlevel = new JLabel("");
    JLabel pocetstriel = new JLabel("" + strela.pocetstriel());
    JLabel pocetzivotov = new JLabel("" + l.zivot);
    JLabel ukazskore = new JLabel("Skore: " + skore);
    int pocetponoriek = 0;
    int level = 1;
    int predlevel = 0;
    int kolo[][] = new int[10][3];

    /** Creates new form Panel */
    public Panel() {
        JOptionPane.showMessageDialog(this, "Ovládanie:\nDolava: A\nDoprava: D\nStrela: K");
        int rychlost = 10;
        int ponorky = 0;
        for (int i = 0; i < 10; i++) {
            kolo[i][0] = i;
            kolo[i][1] = rychlost--;
            kolo[i][2] = ponorky++;
        }
        this.add(l);
        this.setBounds(0, 0, 400, 300);
        t.start();
        ukazlevel.setBounds(190, 0, 50, 15);
        this.add(ukazlevel);
        pocetzivotov.setIcon(zivotlabel);
        pocetstriel.setIcon(strelalabel);
        this.add(slnko);
        pocetzivotov.setBounds(375, 0, 50, 15);
        this.add(pocetzivotov);
        pocetstriel.setBounds(5, 0, 50, 15);
        this.add(pocetstriel);
        ukazskore.setBounds(175, 20, 100, 15);
        this.add(ukazskore);
        hudba();
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();

        setOpaque(false);

        jPanel1.setOpaque(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

    final public void hudba() {
        try {
            Sequence sequence = MidiSystem.getSequence(new FileInputStream("zvuky/theme.mid"));
            Sequencer sequencer = MidiSystem.getSequencer();
            sequencer.open();
            sequencer.setSequence(sequence);
            sequencer.start();
            sequencer.setLoopCount(99);
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public void leveldone() {

        AudioPlayer ap = AudioPlayer.player;
        AudioStream as;
        AudioData ad;
        try {
            InputStream test = new FileInputStream("./zvuky/level.wav");
            as = new AudioStream(test);
            AudioPlayer.player.start(as);

        } catch (FileNotFoundException e) {
            System.out.print(e.toString());
        } catch (IOException error) {
            System.out.print(error.toString());
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //aktualizádia skore:
        ukazskore.setText("Skore: " + (skore - 1));
        ukazlevel.setText("Level: " + level);
        //časový odpočet
        cas--;
        skore--;
        //pridávanie ponoriek
        if (predlevel < level && level < 11) {
            predlevel = level;
            int l = 10 - level;
            for (int i = l; i < 10; i++) {
                int x = (int) (10 + Math.random() * 311);
                int y = (int) (100 + Math.random() * 150);
                ponorka = new Ponorka(x, y, this, kolo[i][1]);
                this.add(ponorka);
                pocetponoriek++;
                ponorkypole.add(ponorka);
            }
        }
        //strielanie lodky do ponoriek:
        ArrayList strely = Lodka.getstrely();
        pocetstriel.setText("" + (5 - strela.pocetstriel()));
        for (int i = 0; i < strely.size(); i++) {
            Strela m = (Strela) strely.get(i);
            for (int p = 0; p < ponorkypole.size(); p++) {
                Ponorka ponor = (Ponorka) ponorkypole.get(p);
                if (m.getRectangle() == null) {
                } else {
                    if (m.getRectangle().intersects(ponor.rectangle)) {
                        ponor.vybuchni();
                        m.zmizni();
                        strely.remove(m);
                        ponorkypole.remove(ponor);
                        skore = skore + 200;
                        pocetponoriek--;
                        if (pocetponoriek == 0 && level < 11) {
                            t.stop();
                            l.zastavpohyb();
                            JOptionPane.showMessageDialog(this, "Prešiel si: " + level + " level.");
                            skore = skore + cas + 500;
                            cas = 2400;
                            level++;
                        }
                        t.start();
                        repaint();
                    }
                }
            }
        }
        //strielanie ponoriek do lodky:
        for (int i = 0; i < ponorkypole.size(); i++) {
            Ponorka p = (Ponorka) ponorkypole.get(i);
            ArrayList ponorkastrely = p.getstrely();
            for (int x = 0; x < ponorkastrely.size(); x++) {
                strelaPonorka s = (strelaPonorka) ponorkastrely.get(x);
                if (s.rectangle == null) {
                } else {
                    if (s.rectangle.intersects(l.rectangle)) {
                        s.zmizni();
                        l.zivot--;
                        l.zasah();
                        pocetzivotov.setText("" + l.zivot);
                        if (l.zivot == 0) {
                            l.zastavpohyb();
                            int odpoved = JOptionPane.showConfirmDialog(this, "GAME OVER\nVaše skóre je: " + skore + "\nHrať znovu?");
                            if (odpoved == JOptionPane.YES_OPTION) {
                                t.stop();
                                for (int pp = 0; pp <= ponorkypole.size();) {
                                    try {
                                        Ponorka zmaz = (Ponorka) ponorkypole.get(pp);

                                        for (int ss = 0; ss <= zmaz.strely.size();) {
                                            try {
                                                strelaPonorka zmazs = (strelaPonorka) zmaz.strely.get(ss);
                                                zmazs.zmizni();
                                                this.remove(ss);
                                            } catch (Exception ex) {
                                            }
                                            ss++;
                                        }
                                        zmaz.zmizni();
                                        this.remove(zmaz);
                                    } catch (Exception ex) {
                                    }
                                    pp++;
                                }
                                ponorkypole.clear();
                                this.add(l);
                                this.add(ukazlevel);
                                this.add(pocetstriel);
                                this.add(pocetzivotov);
                                this.add(ukazskore);
                                level = 1;
                                predlevel = 0;
                                l.lodkastart();
                                l.zivot = 3;
                                l.zastavpohyb();
                                pocetzivotov.setText("" + l.zivot);
                                skore = 2400;
                                cas = 2400;
                                pocetponoriek = 0;
                                t.start();
                            } else if (odpoved == JOptionPane.NO_OPTION || odpoved == JOptionPane.CANCEL_OPTION) {
                                System.exit(0);
                            }
                        }
                    }
                }
            }
        }



        if (level == 11) {

            t.stop();
            leveldone();
            l.zastavpohyb();
            int odpoved = JOptionPane.showConfirmDialog(this, "Gratulujem vaše skóre je: " + skore + "\nHrať znovu?");
            if (odpoved == JOptionPane.YES_OPTION) {
                ponorkypole.clear();
                this.add(l);
                this.add(ukazlevel);
                this.add(pocetstriel);
                this.add(pocetzivotov);
                this.add(ukazskore);
                level = 1;
                predlevel = 0;
                skore = 2400;
                cas = 2400;
                l.lodkastart();
                l.zivot = 3;
                l.zastavpohyb();
                pocetzivotov.setText("" + l.zivot);
                pocetponoriek = 0;
                t.start();
            } else if (odpoved == JOptionPane.NO_OPTION || odpoved == JOptionPane.CANCEL_OPTION) {
                System.exit(0);
            }
        }
        if (cas == 0) {
            l.zastavpohyb();
            JOptionPane.showMessageDialog(this, "GAME OVER\nVaše skore je: " + skore);
            System.exit(0);
        }

        //zastavenie po levely 10
        if (level > 10) {
            return;
        }
        pocetstriel.setText("" + (5 - strela.pocetstriel()));
        repaint();
    }
}
